package com.chiyou.common.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

public class PropertiesUtils {
//	private  Log log = LogFactory.getLog(PropertiesUtils.class);
	private static Properties properties;
	private static  String fileName;

	public PropertiesUtils() {

	}

	public static  Properties getInstance(String path) {
		if (null == properties)
			properties = new Properties();
		fileName = path;
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(new File(fileName)));
			properties.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return properties;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String getFileName() {
		return fileName;
	}

	public static  void setFileName(String path) {

		fileName = path;
	}

	private PropertiesUtils(String fileName) {
		try {

			this.fileName = fileName;
			properties = new Properties();
			properties.load(getClass().getClassLoader().getResourceAsStream(fileName));
		} catch (Exception e) {
//			log.error(e.getMessage(), e);
		}
	}

	public  String getProperty(String key) {
		String value = properties.getProperty(key);
		return value;
	}

	public static  void setProperty(String key, String value) {
		try {
			// String path =
			// Properties.class.getClassLoader().getResource(fileName).getFile();

			OutputStream out = new FileOutputStream(fileName);
			try {
				properties.put(key, value);
				properties.store(out, "");
			} catch (Exception e) {
//				log.error(e.getMessage(), e);
				properties.remove(key);
			} finally {
				out.close();
			}
		} catch (Exception e) {
//			log.error(e.getMessage(), e);
		}

	}

	public  void main(String[] args) {



	}

}
